package com.fcscs.cservice.client.main.configuration.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.domain.IdNameModel;
import com.fcscs.core.domain.Property;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.TransactionType;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.TransactionTypeModelData;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.google.gwt.user.client.ui.Widget;

public class TransactionTypePanel extends FcsPanel {
	private VerticalPanel vp;
	private EasyClearCombobox<PropertyIdNameModelData> propCombox;
	private ListStore<PropertyIdNameModelData> propListStore;
	private TransactionTypeGrid grid;
	private TransactionTypeDialog transactionTypeDialog;
	private FormButton addBtn;
	private List<IdNameModel<Property>> propGroupIdNames;

	public TransactionTypePanel(String id) {
		super(id);
	}

	@Override
	public void init() {
		if (null == vp) {
			vp = new VerticalPanel();
			vp.setId("TransactionTypePanel_vp");
			vp.setSpacing(5);
			
			HorizontalPanel hp = new HorizontalPanel();
			hp.setId("TransactionTypePanel_hp");
			hp.setSpacing(5);
			
			Label propLabel = new Label(CoreConstants.CORE_CONSTANTS.property()
					+ CoreConstants.REQURIED_FIELD_LABEL);
			propLabel.setId("TransactionTypePanel_propLabel");
			propLabel.setWidth(150);
			
			propListStore = new ListStore<PropertyIdNameModelData>();
			propCombox = new EasyClearCombobox<PropertyIdNameModelData>();
			propCombox.setId("TransactionTypePanel_propCombox");
			// propCombox.setFieldLabel(CoreConstants.CORE_CONSTANTS.property());
			propCombox.setWidth(150);
			propCombox.setDisplayField(CoreConstants.FULLTNAME);
			propCombox.setAllowBlank(false);
			propCombox.setForceSelection(true);
			propCombox.setTriggerAction(TriggerAction.ALL);
			propCombox.setStore(propListStore);
			propCombox.addSelectionChangedListener(new SelectionChangedListener<PropertyIdNameModelData>() {

				@Override
				public void selectionChanged(SelectionChangedEvent<PropertyIdNameModelData> se) {
					PropertyIdNameModelData pinmmd = propCombox.getValue();
					if (pinmmd != null) {
						newSearch();
					}
				}
			});
			
			hp.add(propLabel);
			hp.add(propCombox);
			vp.add(hp);
			
			grid = TransactionTypeGrid.getInstance(CServiceConstants.CSERVICE_CONSTANTS.transactionType(), null);
			vp.add(grid);
			
			addBtn = new FormButton(CoreConstants.CORE_MESSAGES.
					operation(CoreConstants.CORE_CONSTANTS.add(),CServiceConstants.CSERVICE_CONSTANTS.transactionType()));
			addBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					transactionTypeDialog = TransactionTypeDialog.getInstance(true);
					transactionTypeDialog.show();
				}
			});
			
			vp.add(addBtn);
			add(vp);
			onShow();
		}

	}
	
	public void newSearch() {
		grid.removeAllModels();
		List<TransactionTypeModelData> transactionTypeList = new ArrayList<TransactionTypeModelData>();
		TransactionType model1 = new TransactionType();
		model1.setTransactionType("Transaction Type1");
		model1.setCode("code1");
		model1.setRevenue("Revenue1");
		transactionTypeList.add(new TransactionTypeModelData(model1));

		TransactionType model2 = new TransactionType();
		model2.setTransactionType("Transaction Type2");
		model2.setCode("code2");
		model2.setRevenue("Revenue2");
		transactionTypeList.add(new TransactionTypeModelData(model2));
		grid.addModels(transactionTypeList);
	}

	private void loadProperty() {
		propGroupIdNames = SessionContext.getInstance().getPropertyGroup();
		propListStore.removeAll();
		if (0 != propGroupIdNames.size()) {
			int propIndex = 0;
			for (IdNameModel<Property> inm : propGroupIdNames) {
				propListStore.add(new PropertyIdNameModelData(inm));
				if (propIndex == 0) {
					if (SessionContext.getInstance().getPropertyId() == inm.getId()) {
						propIndex = propListStore.getCount() - 1;
					}
				}
			}
			propCombox.setValue(propListStore.getAt(propIndex));
		} else {
			IdNameModel<Property> tempINM = new IdNameModel<Property>(SessionContext.getInstance()
					.getPropertyId(), SessionContext.getInstance().getPropertyName());
			propListStore.add(new PropertyIdNameModelData(tempINM));
			propCombox.setValue(propListStore.getAt(0));
		}
	}

	@Override
	public UserAuthority getRequiredAuthority() {
		return null;
	}

	@Override
	protected Widget getViewPanel() {
		return null;
	}

	@Override
	public void onShow() {
		loadProperty();
	}

	@Override
	public boolean onHide() {
		return true;
	}

}
